package com.slz.crm.server.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.slz.crm.common.untils.BaseUnit;
import com.slz.crm.pojo.ao.RoleAO;
import com.slz.crm.pojo.entity.RoleEntity;
import com.slz.crm.pojo.vo.RoleVO;
import com.slz.crm.server.mapper.RoleMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RoleServiceImpl implements com.slz.crm.server.service.RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public Page<RoleVO> list(Integer pageNum, Integer pageSize) {

        // 分页查询角色列表
        Page<RoleEntity> page = roleMapper.selectPage(new Page<>(pageNum, pageSize), null);

        // 转换为VO列表
        List<RoleVO> roleVOList = page.getRecords().stream()
                .map(RoleVO::fromEntity)
                .toList();

        // 设置分页信息
        Page<RoleVO> roleVOPage = new Page<>();
        BeanUtils.copyProperties(page, roleVOPage);
        roleVOPage.setRecords(roleVOList);

        return roleVOPage;
    }

    @Override
    public RoleVO getMyRole() {
        RoleAO role = BaseUnit.getCurrentRole();

        RoleEntity roleEntity = roleMapper.selectById(role.getRoleId());

        return RoleVO.fromEntity(roleEntity);
    }
}
